﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MovieTweet;

namespace MovieTweet
{

	public partial class FrmMovieDetails : Form
	{
		private string movieId;
		private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
		private DataTable dtTweets;


		public FrmMovieDetails(string movieId)
		{
			InitializeComponent();
			this.movieId = movieId;

			this.Text = MovieDetails.title = "";
			lblTitle.Text = MovieDetails.title = "";
			lblDirector.Text = MovieDetails.director = "";
			lblYear.Text = MovieDetails.year = "";
			lblGenre.Text = MovieDetails.genre = "";
			lblDuration.Text = MovieDetails.duration = "";
			lblRating.Text = MovieDetails.rating = "";
			lblActors.Text = MovieDetails.actors = "";
			txtSynopsis.Text = MovieDetails.plot = "";
			lblMentions.Text = MovieDetails.mentions = "0";
			pctPoster.Image = MovieDetails.image = null;

			MovieDetails.dtTweets = new DataTable();
			MovieDetails.dtTweets.Columns.Add("User");
			MovieDetails.dtTweets.Columns.Add("Time");
			MovieDetails.dtTweets.Columns.Add("Text");
			
			MovieDetails.loaded = false;

			timer.Interval = 100;
			timer.Tick += new EventHandler(timer_Tick);
		}

		void timer_Tick(object sender, EventArgs e)
		{
			this.Text = MovieDetails.title + " - LOADING...";
			lblTitle.Text = MovieDetails.title;
			lblDirector.Text = MovieDetails.director;
			lblYear.Text = MovieDetails.year;
			lblGenre.Text = MovieDetails.genre;
			lblDuration.Text = MovieDetails.duration;
			lblRating.Text = MovieDetails.rating;
			lblActors.Text = MovieDetails.actors;
			txtSynopsis.Text = MovieDetails.plot;
			lblMentions.Text = MovieDetails.mentions;
			pctPoster.Image = MovieDetails.image;

			if (MovieDetails.loaded)
			{
				timer.Stop();
				this.Text = MovieDetails.title;
				
				//this.dtTweets = new DataTable();
				//for (int i = 0; i < MovieDetails.dtTweets.Rows.Count; i++)
				//    this.dtTweets.Rows.Add(MovieDetails.dtTweets.Rows[i]);
				this.dtTweets = MovieDetails.dtTweets;
				
				dgvTweets.DataSource = this.dtTweets;
				dgvTweets.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
				dgvTweets.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
				dgvTweets.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
				dgvTweets.Columns[1].Width = 40;
			}
		}

		private void FrmMovieDetails_Load(object sender, EventArgs e)
		{
			Thread workerThread = new Thread(getMovieInfo);
			workerThread.Start();
			timer.Start();
		}

		private void getMovieInfo()
		{
			RottenInfo info = new RottenInfo("Movie");
			RottenResult movieInfo = info.getMovie(movieId);

			MovieDetails.title = movieInfo.title;
			MovieDetails.year = movieInfo.year;

			foreach (string genre in movieInfo.genres)
				MovieDetails.genre += genre + ", ";
			MovieDetails.genre = MovieDetails.genre.Substring(0, MovieDetails.genre.Length - 2);
			MovieDetails.genre = MovieDetails.genre.Replace("&", "&&");

			foreach (Person actor in movieInfo.abridged_cast)
				MovieDetails.actors += actor.name + ", ";
			MovieDetails.actors = MovieDetails.actors.Substring(0, MovieDetails.actors.Length - 2);

			MovieDetails.plot = movieInfo.synopsis;

			foreach (Person director in movieInfo.abridged_directors)
				MovieDetails.director += director.name + ", ";
			MovieDetails.director = MovieDetails.director.Substring(0, MovieDetails.director.Length - 2);

			MovieDetails.duration = movieInfo.runtime + " min";
			MovieDetails.rating = "Critics: " + movieInfo.ratings.critics_score + ", Audience: " + movieInfo.ratings.audience_score;
			MovieDetails.image = downloadImage(movieInfo.posters.detailed);

			TwitterInfo tweetInfo = new TwitterInfo();
			int mentions = tweetInfo.countMentions(movieInfo.title);
			MovieDetails.mentions = mentions.ToString();

			foreach (TwitterResult result in tweetInfo.getLatest(movieInfo.title, 10))
			{
				object[] objectResult = new object[] { result.from_user, result.created_at, result.text };
				MovieDetails.dtTweets.Rows.Add(objectResult);
			}

			MovieDetails.loaded = true;

			
		}

		private Image downloadImage(string url)
		{
			System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
			request.AllowWriteStreamBuffering = true;
			System.Net.WebResponse response = request.GetResponse();
			System.IO.Stream stream = response.GetResponseStream();
			return Image.FromStream(stream);
		}

		private void btnTrailer_Click(object sender, EventArgs e)
		{
			YouTubeInfo info = new YouTubeInfo();
			string url = info.getVideoURL(MovieDetails.title);
			//MessageBox.Show(url);

			string key = @"htmlfile\shell\open\command";
			Microsoft.Win32.RegistryKey registryKey =
				Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(key, false);
			string browserPath =  ((string) registryKey.GetValue(null, null)).Split('"')[1];

			try
			{
				System.Diagnostics.Process.Start(browserPath, url);
			}
			catch (Exception exp)
			{
				MessageBox.Show(exp.Message);
			}

		}
	}

	public static class MovieDetails
	{
		public static string title;
		public static string director;
		public static string year;
		public static string genre;
		public static string duration;
		public static string rating;
		public static string actors;
		public static string plot;
		public static Image image;

		public static string mentions;
		public static DataTable dtTweets;

		public static bool loaded;
	}
}
